﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.Profinet.AllenBradley;

namespace CastingWellEquipmentGUI.Utils
{
    /// <summary>
    /// 与设备通讯类
    /// </summary>
    public class DeviceCommunication
    {

        public DeviceCommunication()
        {

        }

        public bool SetTagValue(string brandName, string plcModelName, string iP, string address, string dataType, string value)
        {
            bool result;
            try
            {
                switch (brandName)
                {
                    case "AB":
                        AllenBradleyNet allenBradley = new AllenBradleyNet(iP);
                        OperateResult operate = allenBradley.ConnectServer();
                        if (operate.IsSuccess)
                        {
                            switch (dataType)
                            {
                                case "Bool":
                                    if (value.ToUpper() == "TRUE")
                                    {
                                        allenBradley.Write(address, true);
                                    }
                                    else if (value.ToUpper() == "FALSE")
                                    {
                                        allenBradley.Write(address, false);
                                    }
                                    break;
                                case "Int":
                                    allenBradley.Write(address, int.Parse(value));
                                    break;
                                case "Real":
                                    allenBradley.Write(address, double.Parse(value));
                                    break;
                                default:
                                    break;
                            }
                            result = true;
                            allenBradley.ConnectClose();
                        }
                        else
                        {
                            result = false;
                        }
                        break;

                    default:
                        result = false;
                        break;
                }
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }

        public object GetTagValue(string brandName, string plcModelName, string iP, string address, string dataType)
        {
            object result = null;
            try
            {
                switch (brandName)
                {
                    case "AB":
                        AllenBradleyNet allenBradley = new AllenBradleyNet(iP);
                        OperateResult operate = allenBradley.ConnectServer();
                        if (operate.IsSuccess)
                        {
                            switch (dataType)
                            {
                                case "Bool":
                                    result = allenBradley.ReadBool(address);
                                    break;
                                case "Int":
                                    result = allenBradley.ReadInt32(address);
                                    break;
                                case "Real":
                                    result = allenBradley.ReadDouble(address);
                                    break;
                                default:
                                    break;
                            }
                            allenBradley.ConnectClose();
                        }
                        else
                        {
                            result = null;
                        }
                        break;

                    default:
                        result = null;
                        break;
                }
            }
            catch (Exception e)
            {
                result = null;
            }
            return result;
        }
    }
}
